using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class THocSinhMonVanHoaDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the tHocSinhMonVanHoa table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, THocSinhMonVanHoaDTO tHocSinhMonVanHoa)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhMonVanHoa", tHocSinhMonVanHoa);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhMonVanHoa.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.MVH_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.CDVH_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_1, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_2, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_1, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_2, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemtongket, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemcodinh, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonVanHoaInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the tHocSinhMonVanHoa table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, THocSinhMonVanHoaDTO tHocSinhMonVanHoa)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhMonVanHoa", tHocSinhMonVanHoa);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhMonVanHoa.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.MVH_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.CDVH_ID, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_1, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_2, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso1_3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_1, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_2, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso2_3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemheso3, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemtongket, DBNull.Value),
				Null.GetNull(tHocSinhMonVanHoa.Diemcodinh, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonVanHoaUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhMonVanHoa table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int hS_ID, int mVH_ID, int cDVH_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mVH_ID, DBNull.Value),
Null.GetNull(cDVH_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonVanHoaDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhMonVanHoa table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonVanHoaDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonVanHoaDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the tHocSinhMonVanHoa table.
		/// </summary>
		public static THocSinhMonVanHoaDTO Select(Database objDb, int hS_ID, int mVH_ID, int cDVH_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mVH_ID, DBNull.Value),
Null.GetNull(cDVH_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the tHocSinhMonVanHoa table.
		/// </summary>
		public static DataTable Select(Database objDb, int hS_ID, int mVH_ID, int cDVH_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mVH_ID, DBNull.Value),
Null.GetNull(cDVH_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the tHocSinhMonVanHoa table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("tHocSinhMonVanHoaSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the tHocSinhMonVanHoa table.
		/// </summary>
		public static List<THocSinhMonVanHoaDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMonVanHoa table.
		/// </summary>
		public static List<THocSinhMonVanHoaDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMonVanHoa table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMonVanHoa table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonVanHoaSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the THocSinhMonVanHoaDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static THocSinhMonVanHoaDTO MapDataReader(IDataReader dataReader)
		{
			THocSinhMonVanHoaDTO tHocSinhMonVanHoaDTO = new THocSinhMonVanHoaDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return tHocSinhMonVanHoaDTO;
			tHocSinhMonVanHoaDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["HS_ID"]);
			tHocSinhMonVanHoaDTO.MVH_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["MVH_ID"]);
			tHocSinhMonVanHoaDTO.CDVH_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["CDVH_ID"]);
			tHocSinhMonVanHoaDTO.Diemheso1_1 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso1_1"]);
			tHocSinhMonVanHoaDTO.Diemheso1_2 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso1_2"]);
			tHocSinhMonVanHoaDTO.Diemheso1_3 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso1_3"]);
			tHocSinhMonVanHoaDTO.Diemheso2_1 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso2_1"]);
			tHocSinhMonVanHoaDTO.Diemheso2_2 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso2_2"]);
			tHocSinhMonVanHoaDTO.Diemheso2_3 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso2_3"]);
			tHocSinhMonVanHoaDTO.Diemheso3 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemheso3"]);
			tHocSinhMonVanHoaDTO.Diemtongket = clsCommon.fncCnvNullToFloat(dt.Rows[0]["diemtongket"]);
			tHocSinhMonVanHoaDTO.Diemcodinh = clsCommon.fncCnvNullToBool(dt.Rows[0]["diemcodinh"]);

			return tHocSinhMonVanHoaDTO;
		}
		/// <summary>
		/// Creates a new instance of the THocSinhMonVanHoaDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<THocSinhMonVanHoaDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<THocSinhMonVanHoaDTO> lstResult = new List<THocSinhMonVanHoaDTO>();
			THocSinhMonVanHoaDTO tHocSinhMonVanHoaDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			tHocSinhMonVanHoaDTO = new THocSinhMonVanHoaDTO();
			tHocSinhMonVanHoaDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["HS_ID"]);
			tHocSinhMonVanHoaDTO.MVH_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["MVH_ID"]);
			tHocSinhMonVanHoaDTO.CDVH_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["CDVH_ID"]);
			tHocSinhMonVanHoaDTO.Diemheso1_1 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso1_1"]);
			tHocSinhMonVanHoaDTO.Diemheso1_2 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso1_2"]);
			tHocSinhMonVanHoaDTO.Diemheso1_3 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso1_3"]);
			tHocSinhMonVanHoaDTO.Diemheso2_1 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso2_1"]);
			tHocSinhMonVanHoaDTO.Diemheso2_2 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso2_2"]);
			tHocSinhMonVanHoaDTO.Diemheso2_3 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso2_3"]);
			tHocSinhMonVanHoaDTO.Diemheso3 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemheso3"]);
			tHocSinhMonVanHoaDTO.Diemtongket = clsCommon.fncCnvNullToFloat(dt.Rows[i]["diemtongket"]);
			tHocSinhMonVanHoaDTO.Diemcodinh = clsCommon.fncCnvNullToBool(dt.Rows[i]["diemcodinh"]);
lstResult.Add(tHocSinhMonVanHoaDTO);
}

			return lstResult;
		}

		#endregion
	}
}
